<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='管理中心',active='setting'">
<header th:replace="back/header::headerFragment(${title},${active})"></header>
<head>
    <meta th:name="_csrf" th:content="${_csrf.token}"/>
    <!-- 默认的header name是X-CSRF-TOKEN -->
    <meta th:name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<body class="fixed-left">
<div id="wrapper">
    <div th:replace="back/header::header-body"></div>
    <div class="content-page">
        <div class="content">
            <div class="container">
                <div class="row">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <h4 class="panel-title">创建IP封禁模式</h4>
                                </div>
                                <form id="ipBanModeForm">
                                <div class="panel-body">
                                    <h4 class="panel-title">名称</h4>
                                    <input type="text" class="form-control" placeholder="请输入IP封禁模式名称（必须）" name="modeName" required="required" aria-required="true">
                                    <h4 class="panel-title">单位时间</h4>
                                    <input type="text" class="form-control" placeholder="请输入单位时间（必须/单位：分钟）" name="times" required="required" aria-required="true">
                                    <h4 class="panel-title">最大次数</h4>
                                    <input type="text" class="form-control" placeholder="请输入单位时间内最大操作次数（必须）" name="maxRequests" required="required" aria-required="true">
                                    <br>
                                    <button type="button" class="btn btn-primary waves-effect waves-light" onclick="createIPBanMode();">
                                        保存
                                    </button>
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    <button type="button" class="btn btn-primary waves-effect waves-light" onclick="closeAllMode();">
                                        关闭IP封禁模式
                                    </button>

                                </div>
                                </form>
                            </div>
                        </div>

                        <div class="col-md-4">

                            <div class="col-md-4">
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h4 class="panel-title">访问量</h4>
                                    </div>
                                            <li class="list-group-item">
                                                今日访问量：<span class="counter" th:text="${webRequests}"></span>
                                            </li>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="col-md-12">

                            <table class="table table-striped table-bordered">
                                <thead>
                                <tr>
                                    <th>id</th>
                                    <th>名称</th>
                                    <th>单位时间</th>
                                    <th>最大次数</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <th:block th:each="ipBanMode : ${ipBanModes.list}">
                                    <tr th:id="${ipBanMode.id}">
                                        <td><th:block th:text="${ipBanMode.id}"/></td>
                                        <td><th:block th:text="${ipBanMode.modeName}"/></td>
                                        <td><th:block th:text="${ipBanMode.times}"/></td>
                                        <td><th:block th:text="${ipBanMode.maxRequests}"/></td>
                                        <td><th:block th:text="${ipBanMode.openBan} == true ? '开启' : '关闭'" /></td>
                                        <td>
                                            <a href="javascript:void(0)" th:onclick="'openMode('+${ipBanMode.id}+');'"
                                               class="btn btn-primary btn-sm waves-effect waves-light m-b-5">
                                                <i class="fa fa-check-square-o"></i> <span>开启</span></a>
                                            <a href="javascript:void(0)" th:onclick="'closeAllMode();'"
                                               class="btn btn-danger btn-sm waves-effect waves-light m-b-5">
                                                <i class="fa fa-power-off"></i> <span>关闭</span></a>
                                            <a href="javascript:void(0)" th:onclick="'deleteMode('+${ipBanMode.id}+');'"
                                               class="btn btn-danger btn-sm waves-effect waves-light m-b-5">
                                                <i class="fa fa-trash-o"></i> <span>删除</span></a>
<!--                                            <a class="btn btn-warning btn-sm waves-effect waves-light m-b-5" href="javascript:void(0)">-->
<!--                                                <i class="fa fa-rocket"></i> <span>预览</span></a>-->
                                        </td>
                                    </tr>
                                </th:block>

                                </tbody>
                            </table>
                        </div>
                </div>
                <div th:replace="back/footer :: footer-content"></div>
            </div>
        </div>
    </div>
</div>
<div th:replace="back/footer :: footer"></div>
</div>
<script type="text/javascript">
    /**
     * 保存模式
     * @param status
     */
    function createIPBanMode() {
        var modeName = $('#ipBanModeForm input[name=modeName]').val();
        var times = $('#ipBanModeForm input[name=times]').val();
        var maxRequests = $('#ipBanModeForm input[name=maxRequests]').val();
        if (modeName == '') {
            window.alert('请输入模式名称');
            return;
        }
        if (modeName .length>20) {
            window.alert('模式名称不能超过20个字符！');
            return;
        }
        if (times == '') {
            window.alert('请输入单位时间');
            return;
        }
        if (maxRequests == '') {
            window.alert('请输入最大次数');
            return;
        }
        var params = $("#ipBanModeForm").serialize();
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        var url = '/admin/ipbanmode/create';
        // var url = $('#ipBanModeForm #id').val() != '' ? '/admin/article/modify' : '/admin/ipbanmode/create';
        $.post({
            url:url,
            data:params,
            beforeSend : function(xhr) {
                xhr.setRequestHeader(header, token);
            },
            success: function (result) {
                if (result && result.success) {
                    window.alert("创建成功");
                    window.location.reload();
                } else {
                    window.alert(result.msg)
                }
            }
        });
    }
    /**
     * 关闭所有模式
     * @param status
     */
    function closeAllMode() {
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        var url = '/admin/ipbanmode/closeall';
        if(confirm('确定关闭IP封禁模式吗?')) {
            $.post({
                url: url,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader(header, token);
                },
                success: function (result) {
                    if (result && result.success) {
                        alert('关闭成功!');
                        window.location.reload();
                    } else {
                        window.alert(result.msg)
                    }
                }
            });
        }
    }
    /**
     * 开启一个模式
     * @param id
     */
    function openMode(id) {
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        var url = '/admin/ipbanmode/open';
        if (confirm('确定开启该模式吗?')) {
            $.post({
                url: url,
                data: {id: id},
                beforeSend: function (xhr) {
                    xhr.setRequestHeader(header, token);
                },
                success: function (result) {
                    if (result && result.success) {
                        alert('开启成功!');
                        window.location.reload();
                    } else {
                        window.alert(result.msg)
                    }
                }
            });
        }
    }
    /**
     * 删除一个模式
     * @param id
     */
    function deleteMode(id) {
        var token = $("meta[name='_csrf']").attr("content");
        var header = $("meta[name='_csrf_header']").attr("content");
        var url = '/admin/ipbanmode/delete';
        if (confirm('确定删除该模式吗?')) {
            $.post({
                url: url,
                data: {id: id},
                beforeSend: function (xhr) {
                    xhr.setRequestHeader(header, token);
                },
                success: function (result) {
                    if (result && result.success) {
                        alert('删除成功!');
                        window.location.reload();
                    } else {
                        window.alert(result.msg)
                    }
                }
            });
        }
    }
</script>
</body>
</html>